#include "StrokeManager.h"

CStrokeManager::CStrokeManager()
{

}

CStrokeManager::~CStrokeManager()
{

}

CVertex2DIter CStrokeManager::GetStrokeBeginIterator()
{
	return m_strokes.begin();
}

CVertex2DIter CStrokeManager::GetStrokeEndIterator()
{
	return m_strokes.end();
}

bool CStrokeManager::IsLine()
{
	int count = m_strokes.size();
	float xAvg = 0.0f, yAvg = 0.0f;
	for (int i = 0; i < count; i++)
	{
		xAvg += m_strokes[i].m_data[0];
		yAvg += m_strokes[i].m_data[1];
	}
	xAvg /= count;
	yAvg /= count;

	//	covariance matrix
	float m00, m01, m10, m11;
	//	m00 m01
	//	m10 m11
	float x, y;
	m00 = m01 = m10 = m11 = 0.0f;
	for (int i = 0; i < count; i++)
	{
		x = m_strokes[i].m_data[0];
		y = m_strokes[i].m_data[1];
		x -= xAvg;
		y -= yAvg;
		m00 += x * x;
		m01 += x * y;
		m10 += y * x;
		m11 += y * y;
	}
	//	compute eigenvalues for the covariance matrix
	float delta = (m00 + m11) * (m00 + m11) - 4 * (m00 * m11 - m10 * m10);
	float lamda1 = (m00 + m11 + sqrt(delta)) / 2;
	float lamda2 = (m00 + m11 - sqrt(delta)) / 2;
	return lamda1 / lamda2 > 100;
}